1 00:00:00,460 --> 00:00:06,430 I believe the next fundamental tool we're going to need to script for our game is the player's flashlight. 2 00:00:06,430 --> 00:00:10,870 When the players are inside of the basement, we need to give them a flashlight that they can pick up 3 00:00:10,870 --> 00:00:15,910 from the floor right here, and we got to make that flashlight functional. 4 00:00:15,910 --> 00:00:20,560 We're also going to have that flashlight slowly drain battery, forcing the players to search for new 5 00:00:20,560 --> 00:00:22,150 batteries throughout the basement. 6 00:00:22,150 --> 00:00:23,680 So let's go ahead and get started. 7 00:00:23,680 --> 00:00:28,180 I've attached this model right here next to the lecture that contains the two animations that we're 8 00:00:28,180 --> 00:00:29,920 going to be using for our flashlight. 9 00:00:29,920 --> 00:00:33,670 So you can go ahead and open up your animation editor on this rig. 10 00:00:33,670 --> 00:00:38,230 And you can go ahead and load the animations that are stored inside of here, which is your idle animation 11 00:00:38,230 --> 00:00:41,440 for the flashlight and the equipped animation for the flashlight. 12 00:00:41,440 --> 00:00:42,940 And they're pretty basic. 13 00:00:42,940 --> 00:00:44,140 That's your equip. 14 00:00:44,140 --> 00:00:46,960 And this is the idle animation which loops forever. 15 00:00:46,960 --> 00:00:52,240 Once you get those animations uploaded and you got the IDs copied, you're going to want to head into 16 00:00:52,240 --> 00:00:54,370 server storage and go to the tools folder. 17 00:00:54,370 --> 00:00:58,900 And you'll go to the flashlight and you'll update them into their respective animations. 18 00:00:58,900 --> 00:01:03,220 So upload the equip animation in here and the idle animation inside of there. 19 00:01:03,790 --> 00:01:08,800 From this point, we can go ahead and open up the flashlight server script and the flashlight local 20 00:01:08,800 --> 00:01:09,460 script. 21 00:01:09,490 --> 00:01:14,980 Now our flashlight, which we have an example of right here, has several parts inside of it. 22 00:01:14,980 --> 00:01:19,480 One of these parts is the light part where our light is going to be emanating out of. 23 00:01:19,480 --> 00:01:25,120 We have an attachment in here that shows the inner circle for the light as well as an outer circle. 24 00:01:25,120 --> 00:01:31,810 And we also have a ring to cast a neat little shadow in the center to make the inner light and the outer 25 00:01:31,810 --> 00:01:34,210 diffused light more distinct from each other. 26 00:01:34,600 --> 00:01:40,210 We also have this surface light restricted to a very small little area, because when it's enabled, 27 00:01:40,210 --> 00:01:42,910 it makes it look like the flashlight is glowing inside. 28 00:01:42,910 --> 00:01:48,730 So for example, if I went and disabled this, as you can see, it doesn't look like the flashlight 29 00:01:48,730 --> 00:01:49,870 is really glowing anymore. 30 00:01:49,870 --> 00:01:52,000 But if I go back and enable it. 31 00:01:52,620 --> 00:01:53,940 And we take a look again. 32 00:01:53,940 --> 00:01:56,850 Now it looks like that our flashlight is glowing more. 33 00:01:57,060 --> 00:02:03,390 Now we already have a script in here for when the players click this flashlight giver, it'll clone 34 00:02:03,390 --> 00:02:05,730 the flashlight tool and put it in their backpack. 35 00:02:05,730 --> 00:02:09,120 And it looks like I named it incorrectly, so I'll just rename it here real quick. 36 00:02:09,120 --> 00:02:11,940 We'll just call it Flashlight Giver script. 37 00:02:11,940 --> 00:02:14,970 This is just a very basic script that we don't need to worry about scripting right now. 38 00:02:14,970 --> 00:02:19,830 It's already completed for us, but the main thing we're going to want to worry about is the flashlight 39 00:02:19,830 --> 00:02:22,110 service script and the flashlight local script. 40 00:02:22,110 --> 00:02:26,340 So inside of the local script, the only service we're going to need is the player service. 41 00:02:27,310 --> 00:02:30,280 And then we can make a whole bunch of references to different variables. 42 00:02:30,280 --> 00:02:34,780 We'll make a reference to the flashlight itself, which will be the script dot parent of this local 43 00:02:34,780 --> 00:02:35,350 script. 44 00:02:35,350 --> 00:02:40,810 We're going to get the guy that's inside of our flashlight, which will display on the screen to show 45 00:02:40,810 --> 00:02:42,700 the player the current battery. 46 00:02:42,700 --> 00:02:45,670 So that is inside of the flashlight. 47 00:02:46,090 --> 00:02:51,880 And since this tool is going to be cloned to their inventory from server storage, we're going to have 48 00:02:51,880 --> 00:02:54,250 to wait for everything to replicate inside of the tool. 49 00:02:54,250 --> 00:02:58,240 So we're going to wait for the flashlight guy inside of our flashlight. 50 00:02:58,840 --> 00:03:03,880 And then we're also going to reference an event inside of the server script of our flashlight, which 51 00:03:03,880 --> 00:03:09,160 we are going to use to tell the server to, you know, enable the flashlight on its end. 52 00:03:09,160 --> 00:03:13,210 So clicked event is equal to flashlight. 53 00:03:13,940 --> 00:03:20,120 And we're going to wait for the flashlight service script, and then we're going to wait for the clicked 54 00:03:20,120 --> 00:03:20,600 event. 55 00:03:21,140 --> 00:03:23,360 From this point, we can make a reference to the local player. 56 00:03:23,360 --> 00:03:28,640 So players dot local player and we're going to get the character of the local player which is equal 57 00:03:28,640 --> 00:03:34,940 to the local player dot character or local player dot character added. 58 00:03:34,940 --> 00:03:37,730 And we'll wait for the player's character in case it doesn't exist. 59 00:03:37,850 --> 00:03:42,140 We're going to get the humanoid of our character, so that's going to be in the character. 60 00:03:42,140 --> 00:03:44,150 Wait for child humanoid. 61 00:03:44,970 --> 00:03:47,370 And they're also going to get the animator. 62 00:03:47,370 --> 00:03:51,030 So in the humanoid we'll wait for child animator. 63 00:03:52,760 --> 00:03:55,100 And we can go ahead and denote this as an animator. 64 00:03:55,100 --> 00:03:57,680 And we can go ahead and denote this as the humanoid. 65 00:03:58,100 --> 00:04:03,260 After that, we can go ahead and load our equip animation so we can call this Equip track. 66 00:04:03,260 --> 00:04:04,430 And we'll use the animator. 67 00:04:04,430 --> 00:04:10,670 And we'll load an animation which is inside of our flashlight wafer child animations, and then wait 68 00:04:10,670 --> 00:04:12,110 for child equip. 69 00:04:12,440 --> 00:04:14,780 And then we're just going to do the exact same thing. 70 00:04:14,780 --> 00:04:22,430 But this time we'll have the idle track and we'll wait for the idle animation. 71 00:04:22,940 --> 00:04:27,200 And since we've already waited for the animations folder up here, we don't have to do it again down 72 00:04:27,200 --> 00:04:27,440 here. 73 00:04:27,440 --> 00:04:30,230 So we can just directly index for the animations folder. 74 00:04:30,650 --> 00:04:34,130 And then we're going to make a reference to the click sound that's inside of the flashlight. 75 00:04:34,130 --> 00:04:38,060 So there's a click on sound which is in the flashlight. 76 00:04:38,660 --> 00:04:43,250 We'll wait for child inside of the handle and we'll wait for the on sound. 77 00:04:43,400 --> 00:04:47,510 And then there's also a click off sound in the same location. 78 00:04:47,510 --> 00:04:51,620 So flashlight dot handle and we'll wait for the off sound. 79 00:04:52,160 --> 00:04:56,810 And then we're going to create two booleans to keep track of different things in the flashlight. 80 00:04:56,840 --> 00:05:02,510 First we're going to have a click debounce to prevent the player from spam turning on and off the flashlight. 81 00:05:02,510 --> 00:05:04,790 And we're also going to verify that on the server as well. 82 00:05:04,790 --> 00:05:06,410 So we'll set this to false by default. 83 00:05:06,410 --> 00:05:10,070 And then we're also going to have a variable to keep track of whether or not the flashlight is on or 84 00:05:10,070 --> 00:05:10,250 off. 85 00:05:10,250 --> 00:05:12,710 So this will be called flashlight on. 86 00:05:12,710 --> 00:05:14,120 And we'll set that to false. 87 00:05:14,720 --> 00:05:20,150 And then from this point we can have a private function to listen for when the flashlight is activated 88 00:05:20,150 --> 00:05:21,440 or the tool is clicked. 89 00:05:21,440 --> 00:05:24,830 So we can call this flashlight activated. 90 00:05:24,920 --> 00:05:28,670 And then inside of the handler section the different events we're going to want to listen to. 91 00:05:28,700 --> 00:05:31,670 First off we want to listen for when our flashlight is equipped. 92 00:05:31,670 --> 00:05:33,500 We'll connect a Lambda function to this. 93 00:05:34,650 --> 00:05:35,760 We want to listen to. 94 00:05:35,760 --> 00:05:37,260 When our flashlight is unequipped. 95 00:05:37,260 --> 00:05:39,360 We'll connect a lambda function to this as well. 96 00:05:39,780 --> 00:05:45,990 We want to listen on our flashlight when an attribute changes, because inside of our flashlight we 97 00:05:45,990 --> 00:05:50,400 have two attributes one that represents the current battery of the flashlight and one that represents 98 00:05:50,400 --> 00:05:52,710 the maximum battery of the flashlight. 99 00:05:52,710 --> 00:05:56,100 And we're going to want to get attribute change signal. 100 00:05:56,100 --> 00:05:59,910 And we want to listen for when the current battery attribute changes. 101 00:05:59,910 --> 00:06:02,970 And when it does we're going to connect a Lambda function to this. 102 00:06:03,450 --> 00:06:06,120 We'll also want to listen for when our flashlight is activated. 103 00:06:06,120 --> 00:06:09,090 And we'll connect our flashlight activated function. 104 00:06:09,690 --> 00:06:14,160 And lastly, we want to listen for when our flashlight is being destroyed. 105 00:06:14,160 --> 00:06:20,220 And the reason for this is if we ever have a server script that destroys the player's tools, like when 106 00:06:20,220 --> 00:06:25,800 they get set to the dead team, then we don't want the guy that we set inside of their player guy folder 107 00:06:25,800 --> 00:06:26,730 to be stuck. 108 00:06:26,730 --> 00:06:30,810 That's a common problem that people have with a lot of tools, because they forget to listen for when 109 00:06:30,810 --> 00:06:35,460 the flashlight is being destroyed, when the flashlight is being destroyed, then the reference that 110 00:06:35,460 --> 00:06:41,100 we made to the guy, we want to go ahead and destroy that guy, no matter where it is, we want to destroy 111 00:06:41,100 --> 00:06:41,400 it. 112 00:06:42,000 --> 00:06:47,610 Okay, so when we equip our flashlight, the first thing we can do is we can set the parent of our guy 113 00:06:47,610 --> 00:06:50,550 equal to the local player dot player guy folder. 114 00:06:50,700 --> 00:06:53,430 We can go ahead and play our equip track. 115 00:06:54,190 --> 00:06:58,660 And then we're also going to play our idle track at the exact same time. 116 00:06:58,660 --> 00:07:02,050 And this is because the animation priority is higher on the equipped track. 117 00:07:02,050 --> 00:07:05,770 So even though we're playing them at the exact same time, this one will play first. 118 00:07:05,770 --> 00:07:09,250 And then once this ends, then this animation will take over. 119 00:07:09,250 --> 00:07:14,680 When our flashlight gets unequipped, we're going to set the g dot parent back into the flashlight. 120 00:07:14,680 --> 00:07:18,100 We're going to stop our equip track just in case it was playing. 121 00:07:18,100 --> 00:07:20,770 And then we're also going to stop our idle track from playing. 122 00:07:21,310 --> 00:07:25,270 Another thing we want to check is whether or not if our flashlight is on. 123 00:07:25,270 --> 00:07:30,580 So if our flashlight is on, then we want to play the click off sound when we unequip our flashlight. 124 00:07:30,580 --> 00:07:36,100 So click off sound and play and then we can set flashlight on equal to false. 125 00:07:36,670 --> 00:07:41,800 Now when the attribute inside of our flashlight changes, then we're going to want to update the guy 126 00:07:41,800 --> 00:07:43,000 that's on our screen. 127 00:07:43,090 --> 00:07:47,170 So what we could do is we could refer to our guy, get the frame. 128 00:07:47,170 --> 00:07:51,460 And there is a text label in there called the current battery label. 129 00:07:51,850 --> 00:07:57,730 And we're going to set the text equal to the flashlight get attribute current battery. 130 00:07:57,820 --> 00:08:04,270 And another thing we want to check in here is if our flashlight get attribute current battery is equal 131 00:08:04,270 --> 00:08:05,110 to zero. 132 00:08:05,110 --> 00:08:11,050 If it is then we want to set flashlight on equal to false. 133 00:08:11,530 --> 00:08:14,590 Now the next function we can fill out is our flashlight activated function. 134 00:08:14,590 --> 00:08:18,850 So when we click we first want to check whether or not our click Debounce is true. 135 00:08:18,850 --> 00:08:21,280 If it is then we're just going to return out of the function. 136 00:08:21,280 --> 00:08:23,740 And then we can set click Debounce equal to true. 137 00:08:24,550 --> 00:08:27,790 And we're going to check if our flashlight is on. 138 00:08:27,790 --> 00:08:29,650 If our flashlight is on in the player clicked. 139 00:08:29,650 --> 00:08:31,630 That means we need to turn off our flashlight. 140 00:08:31,630 --> 00:08:34,600 So flashlight on we're going to set to false. 141 00:08:34,600 --> 00:08:37,180 And then we're going to play the click off sound. 142 00:08:38,420 --> 00:08:41,330 Otherwise, if our flashlight is not on, then we want to turn it on. 143 00:08:41,330 --> 00:08:44,330 So we'll play the click on sound. 144 00:08:46,090 --> 00:08:49,690 And then we'll also set flashlight on equal to true. 145 00:08:51,100 --> 00:08:55,930 After that we can yield for like two seconds, because that's the duration we want to have our cool 146 00:08:55,930 --> 00:08:56,620 down for. 147 00:08:56,620 --> 00:09:00,250 And then we can set click debounce equal to false. 148 00:09:01,070 --> 00:09:06,680 Now, something that's very important now is we need to use our click event to let the server know that 149 00:09:06,680 --> 00:09:12,410 we have activated the tool on our end, so we can do fire server to fire this event. 150 00:09:12,440 --> 00:09:15,770 Now the activated event also works on the server as well. 151 00:09:15,770 --> 00:09:17,990 So there's actually two ways you could do it. 152 00:09:17,990 --> 00:09:22,970 You could either do it through a remote event, or you could just let the server listen to when the 153 00:09:22,970 --> 00:09:24,110 tool is activated. 154 00:09:24,110 --> 00:09:28,940 Technically, either way, it works, but we're going to go ahead and try it out with this remote event. 155 00:09:28,940 --> 00:09:31,580 That way we're just more comfortable with using them. 156 00:09:32,390 --> 00:09:36,710 Now inside of our server script, we're going to need the player service again. 157 00:09:38,920 --> 00:09:41,740 And we're also going to need replicated storage. 158 00:09:45,480 --> 00:09:49,860 We're going to make a reference to the flashlight, which is equal to script dot parent. 159 00:09:49,860 --> 00:09:55,020 And then we're also going to refer to the clicked event which is a child of our script. 160 00:09:55,020 --> 00:09:56,580 So script dot clicked. 161 00:09:57,480 --> 00:10:00,240 We're going to make a reference to the update GUI event. 162 00:10:00,240 --> 00:10:04,560 And we're going to use this to let the player know that, hey, when they run out of battery, we can 163 00:10:04,560 --> 00:10:07,530 give them a small message saying you have no more battery left. 164 00:10:07,530 --> 00:10:14,100 So replicated storage, dot events, dot remotes, dot update guy and then of course we're going to 165 00:10:14,100 --> 00:10:16,920 have to get the GUI actions enum. 166 00:10:16,920 --> 00:10:22,200 So we'll require from replicated storage dot modules dot enums dot gui actions enum. 167 00:10:23,010 --> 00:10:26,310 We're going to make a reference to the inner light inside of our flashlight. 168 00:10:26,310 --> 00:10:28,740 So that's in the flashlight dot light part. 169 00:10:28,740 --> 00:10:29,760 It is an attachment in there. 170 00:10:29,760 --> 00:10:31,530 And we can get the inner light. 171 00:10:31,530 --> 00:10:33,630 And then we're also going to get the outer light. 172 00:10:33,630 --> 00:10:37,350 So that's in the flashlight dot light part dot attachment dot outer. 173 00:10:37,740 --> 00:10:40,920 What we're going to do here is we're going to create a variable called thread. 174 00:10:40,920 --> 00:10:42,420 We're going to set this to nil for now. 175 00:10:42,420 --> 00:10:48,390 But when the flashlight gets equipped and while the flashlight is on, we're going to have a loop that 176 00:10:48,390 --> 00:10:51,270 constantly runs and slowly drains the battery. 177 00:10:51,270 --> 00:10:57,480 And then when the flashlight either runs out of battery or the tools unequipped, that while loop will 178 00:10:57,480 --> 00:11:02,640 either cancel or we will cancel the thread itself when the tool gets unequipped. 179 00:11:02,640 --> 00:11:05,910 We also want a variable to act as a Debounce, so we can call this. 180 00:11:05,910 --> 00:11:08,880 Click Debounce and we'll set this to false. 181 00:11:09,150 --> 00:11:12,120 We'll also have a variable to keep track of whether or not the flashlight is on. 182 00:11:12,120 --> 00:11:14,760 So flashlight on is equal to false. 183 00:11:15,180 --> 00:11:20,280 And then we're also going to have a variable to keep track of when the flashlight was last clicked. 184 00:11:20,280 --> 00:11:24,270 We're going to keep a timestamp of that so we can call it last clicked. 185 00:11:24,270 --> 00:11:27,210 And this is equal to the tick function. 186 00:11:27,570 --> 00:11:33,960 And because this script is going to run immediately and the player might want to turn on their flashlight 187 00:11:33,960 --> 00:11:37,440 immediately, we're going to subtract this tick by two seconds. 188 00:11:37,830 --> 00:11:40,260 Now we can also define some constants for our flashlight. 189 00:11:40,260 --> 00:11:45,150 First off, we want to have a color for when the flashlight is turned on. 190 00:11:45,150 --> 00:11:49,170 So the on color is equal to color three dot from RGB. 191 00:11:49,350 --> 00:11:56,250 And the color that I found best for our flashlight would be 168 on the red, one, 29 on the green, 192 00:11:56,250 --> 00:11:57,660 and then 67 on the blue. 193 00:11:57,690 --> 00:11:59,310 We get this yellowish hue. 194 00:11:59,310 --> 00:12:04,770 And then we also want to define the color for our neon part when the flashlight turns off. 195 00:12:04,770 --> 00:12:06,270 And that's just going to be black. 196 00:12:06,270 --> 00:12:10,320 So color three from RGB 000. 197 00:12:10,590 --> 00:12:14,670 And then we're also going to define the cooldown which is going to be two seconds. 198 00:12:15,060 --> 00:12:17,400 Now of course we're going to need some private functions. 199 00:12:17,400 --> 00:12:20,610 One function is going to change the light. 200 00:12:20,700 --> 00:12:26,490 And we could have a boolean in here like set on to tell this function whether or not we want to change 201 00:12:26,490 --> 00:12:28,080 the lights to be on or off. 202 00:12:28,700 --> 00:12:33,860 We'll have a function to listen to our flashlight event, so we'll just call it Flashlight Event. 203 00:12:33,860 --> 00:12:37,370 And this will get just passed the player that triggered the event. 204 00:12:38,090 --> 00:12:41,480 And then we're also going to have a function and we can call it drain battery. 205 00:12:41,480 --> 00:12:46,250 And this is going to contain the while loop that's going to constantly run to drain the battery. 206 00:12:46,760 --> 00:12:51,080 And inside of the handler section we can listen for when our flashlight is equipped. 207 00:12:51,080 --> 00:12:53,090 We'll connect a lambda function to that. 208 00:12:54,060 --> 00:12:57,150 We can listen to when our flashlight is unequipped. 209 00:12:57,150 --> 00:12:59,100 Connect a function to this as well. 210 00:12:59,980 --> 00:13:02,950 We want to listen for when our clicked event is fired. 211 00:13:02,980 --> 00:13:05,230 So on server event will connect. 212 00:13:06,000 --> 00:13:07,650 Flashlight event. 213 00:13:08,420 --> 00:13:12,890 And then we're also going to want to listen to when the attribute changes on our flashlight. 214 00:13:12,890 --> 00:13:15,560 So get attribute change signal. 215 00:13:16,590 --> 00:13:18,630 And this is going to be the current battery. 216 00:13:19,150 --> 00:13:22,570 And we can connect a function to this. 217 00:13:23,110 --> 00:13:26,110 And we're actually not going to need a main section. 218 00:13:26,110 --> 00:13:27,940 So I'm going to go ahead and delete that. 219 00:13:28,500 --> 00:13:34,500 Okay, so when our flashlight is equipped, the first thing we want to do is call our drain battery 220 00:13:34,500 --> 00:13:40,140 function to start draining the battery when you know the flashlight has no battery in it. 221 00:13:40,470 --> 00:13:44,280 So all we're going to do in here is we're going to have a while loop that runs forever. 222 00:13:44,490 --> 00:13:50,130 And it'll have a yield of we'll say two seconds. 223 00:13:50,130 --> 00:13:54,750 So every two seconds we'll deduct one value off of the current battery. 224 00:13:54,750 --> 00:13:58,320 So it should last a total of 200 seconds with our battery. 225 00:13:58,620 --> 00:14:03,090 And what we're going to do is we're going to check first of all if our flashlight is on. 226 00:14:03,090 --> 00:14:06,510 So if our flashlight is not on then we don't want to drain any battery. 227 00:14:06,510 --> 00:14:08,220 So we'll just continue looping. 228 00:14:09,570 --> 00:14:17,100 If our flashlight, let's say get attribute current battery is somehow less than or equal to zero, 229 00:14:17,100 --> 00:14:19,770 then we're also going to just continue looping. 230 00:14:20,930 --> 00:14:25,580 Otherwise we can set our flashlight the attribute current battery. 231 00:14:26,870 --> 00:14:32,930 And we'll set it to the flashlight, get attribute current battery and subtract that value by one. 232 00:14:33,320 --> 00:14:35,630 And that's all we need to do for our while loop. 233 00:14:35,660 --> 00:14:42,050 So when the flashlight is equipped, we'll use the task dot spawn function to spawn our drain battery 234 00:14:42,050 --> 00:14:43,310 function in a new thread. 235 00:14:44,060 --> 00:14:49,490 And we're going to get that thread returned back to us so we can store it within our thread, uh, variable. 236 00:14:49,700 --> 00:14:55,190 And that way when our flashlight gets unequipped, we can check if thread exists. 237 00:14:55,190 --> 00:15:00,590 So if this thread exists, then we can use the task dot cancel function to stop this while loop from 238 00:15:00,590 --> 00:15:01,400 executing. 239 00:15:01,400 --> 00:15:07,370 Then we can set thread equal to nil and we'll set flashlight on equal to false. 240 00:15:07,370 --> 00:15:12,440 And this is where we would use our change light function to turn off the flashlight. 241 00:15:12,740 --> 00:15:16,910 So in our change light function what we're going to do is we're going to refer to our inner light. 242 00:15:16,910 --> 00:15:19,730 And we're going to set enabled equal to the boolean. 243 00:15:19,730 --> 00:15:23,840 We'll do the same thing for the outer light dot enabled equal to the boolean. 244 00:15:24,490 --> 00:15:30,070 And then our flashlight light part dot color is going to be equal to. 245 00:15:30,100 --> 00:15:32,560 We're going to check set on. 246 00:15:32,560 --> 00:15:35,890 And then we're going to pass on color. 247 00:15:36,430 --> 00:15:43,450 So if set on is true, then the value that gets returned from this and statement is going to be our 248 00:15:43,450 --> 00:15:47,320 on color, which means our color property is going to be set to the correct color. 249 00:15:47,890 --> 00:15:50,620 Or we're going to pass the off color. 250 00:15:51,100 --> 00:15:55,630 So basically we remember we talked about this kind of statement for setting variables. 251 00:15:55,630 --> 00:15:57,460 When set on is equal to true. 252 00:15:57,460 --> 00:16:00,340 The value that gets set here is going to be our on color. 253 00:16:00,370 --> 00:16:04,900 Otherwise if set on evaluates to false then the value that's going to be passed. 254 00:16:04,900 --> 00:16:06,550 Here is our off color. 255 00:16:06,850 --> 00:16:10,900 Alrighty, now the next function we can go ahead and fill out is our flashlight event function. 256 00:16:10,900 --> 00:16:15,070 The first thing we need to check is who exactly fired this event? 257 00:16:15,070 --> 00:16:20,290 Because an exploiter could be firing the events in other players flashlights, so we want to make sure 258 00:16:20,290 --> 00:16:25,090 the player firing this event is the same player that is actually having the flashlight equipped. 259 00:16:25,090 --> 00:16:30,730 So if the player that fired this event is not equal to and we're going to use the player service and 260 00:16:30,730 --> 00:16:36,520 get player from character and we can pass the flashlight dot parent, which should be the character 261 00:16:36,520 --> 00:16:37,120 model. 262 00:16:37,120 --> 00:16:42,040 If this player is not equal to the player who has ownership over the flashlight, then we're just going 263 00:16:42,040 --> 00:16:42,850 to return. 264 00:16:43,450 --> 00:16:50,590 The next thing we're going to check is if the tick, the current tick minus our last tick, or the last 265 00:16:50,590 --> 00:16:53,560 clicked is less than our cooldown. 266 00:16:54,480 --> 00:16:58,350 So if it's been less than two seconds, then we don't want to do anything because we're still waiting, 267 00:16:58,350 --> 00:16:59,610 so we'll just return. 268 00:16:59,910 --> 00:17:03,540 Otherwise we can set last clicked equal to the current tick. 269 00:17:03,570 --> 00:17:05,940 Then we can check if our flashlight is on. 270 00:17:05,940 --> 00:17:11,130 So if it is on and the flashlight event gets fired, then we clearly need to turn off the flashlight. 271 00:17:11,130 --> 00:17:14,220 So flashlight on is going to be set to false. 272 00:17:14,220 --> 00:17:17,880 And then we're going to use our change light function and pass false. 273 00:17:19,270 --> 00:17:20,020 Else if. 274 00:17:20,940 --> 00:17:22,920 Flashlight on is true. 275 00:17:22,920 --> 00:17:30,030 And another condition we want to check is the flashlight get attribute current battery is greater than 276 00:17:30,030 --> 00:17:30,360 zero. 277 00:17:30,360 --> 00:17:32,760 So if the battery is greater than zero, then we're all good to go. 278 00:17:32,760 --> 00:17:34,110 And we can turn on the flashlight. 279 00:17:34,110 --> 00:17:35,340 So flashlight on. 280 00:17:36,100 --> 00:17:40,900 Equal to true and then change light will pass true to that. 281 00:17:41,140 --> 00:17:45,310 Otherwise they are going to be out of battery. 282 00:17:45,310 --> 00:17:51,100 Because if this condition didn't evaluate to true meaning that we do not have enough battery, then 283 00:17:51,100 --> 00:17:55,720 we can use the update GUI event and fire to this particular player here. 284 00:17:55,720 --> 00:17:57,370 So we'll pass the player. 285 00:17:57,950 --> 00:18:05,270 Give the guy actions enum of small message and we can give them a message of like you ran out of batteries. 286 00:18:06,510 --> 00:18:11,790 And then just for a little bit of added protection, we're also going to check if our click Debounce 287 00:18:11,790 --> 00:18:12,180 is true. 288 00:18:12,180 --> 00:18:14,220 If it is, then we're going to return. 289 00:18:14,220 --> 00:18:17,010 And then we can set click Debounce equal to true. 290 00:18:17,830 --> 00:18:23,530 And then we'll go down, and then we're going to yield for our, uh. 291 00:18:23,530 --> 00:18:24,250 What was it? 292 00:18:24,250 --> 00:18:27,520 I believe it was the cooldown. 293 00:18:27,970 --> 00:18:33,010 And then we're going to set click the balance back to false after that cooldown ends. 294 00:18:33,010 --> 00:18:35,410 So we've kind of got double security here. 295 00:18:35,410 --> 00:18:40,990 Technically you could just do one or the other, but why not have more security even if it is a little 296 00:18:40,990 --> 00:18:41,740 redundant? 297 00:18:42,100 --> 00:18:47,530 Now, the last thing we need to fill out is listening to when our current battery attribute changes. 298 00:18:47,530 --> 00:18:53,320 So if the current battery attribute changes, we want to check if the flashlight get attribute current 299 00:18:53,320 --> 00:18:56,950 battery is less than or equal to zero. 300 00:18:56,950 --> 00:18:59,590 If it is, then we need to turn off the flashlight. 301 00:18:59,590 --> 00:19:00,880 So flashlight on. 302 00:19:00,880 --> 00:19:02,620 We're going to set to false. 303 00:19:02,620 --> 00:19:07,120 And then we're going to change light and set that to false as well. 304 00:19:07,120 --> 00:19:07,840 Perfect. 305 00:19:07,840 --> 00:19:11,020 And our flashlight should actually be complete. 306 00:19:11,020 --> 00:19:17,020 So what we could do to test it real quick is I'm going to copy the flashlight and temporarily put it 307 00:19:17,020 --> 00:19:18,280 inside of the starter pack. 308 00:19:18,280 --> 00:19:20,440 And we can go ahead and play test our game. 309 00:19:21,860 --> 00:19:24,800 All right, let's go ahead and test out our flashlight here. 310 00:19:24,800 --> 00:19:27,140 So when we hold it out, there we go. 311 00:19:27,140 --> 00:19:28,610 We equip the flashlight. 312 00:19:28,850 --> 00:19:36,830 And unfortunately, it doesn't look like our torso tilt is quite working right now which is a little 313 00:19:36,830 --> 00:19:41,630 strange, but our flashlight should be tilting along with our torso. 314 00:19:41,630 --> 00:19:45,500 We'll figure that out in a moment, but if we turn on the flashlight, there we go. 315 00:19:45,500 --> 00:19:51,290 We have our bright light, and as you can see, our current battery is draining every two seconds. 316 00:19:51,290 --> 00:19:57,080 If we unequip our flashlight, the flashlight turns off and we shouldn't have any battery drain. 317 00:19:57,080 --> 00:19:58,490 So remember it was at 95. 318 00:19:58,490 --> 00:20:02,330 So let's wait a couple of seconds and let's go ahead and check again. 319 00:20:02,690 --> 00:20:03,230 Perfect. 320 00:20:03,230 --> 00:20:05,150 The current battery is still at 95. 321 00:20:05,150 --> 00:20:10,100 And now we can go ahead and turn our flashlight back on again and look around our map. 322 00:20:11,480 --> 00:20:17,510 So our torso tilt is working, but unfortunately it looks like our other arm is not wanting to follow 323 00:20:17,510 --> 00:20:23,510 along with the tilt of our torso, which is actually kind of strange. 324 00:20:24,320 --> 00:20:29,000 So let me go ahead and figure out why exactly this isn't working, and we can get back to this in a 325 00:20:29,000 --> 00:20:29,780 little bit. 326 00:20:30,980 --> 00:20:31,370 All right. 327 00:20:31,370 --> 00:20:37,040 After a little bit of digging, I actually figured out why our arm was not rotating with the direction 328 00:20:37,040 --> 00:20:38,090 that we were looking. 329 00:20:38,090 --> 00:20:43,670 And the reason was, is that there's an attribute inside of the workspace called retargeting. 330 00:20:43,670 --> 00:20:47,270 And this is a property that you're going to want to disable. 331 00:20:47,270 --> 00:20:53,030 Unfortunately, this property, when enabled, has some confliction when we try to rotate the keyframe 332 00:20:53,030 --> 00:20:55,700 zero of different motor de constraints. 333 00:20:55,700 --> 00:20:59,420 But by disabling this property, we can go ahead and fix that issue. 334 00:20:59,420 --> 00:21:04,730 And now the animations will follow along with the rotation of the different motor 60s that you have 335 00:21:04,730 --> 00:21:06,200 in your player's character. 336 00:21:06,260 --> 00:21:10,910 So all you need to do inside of the workspace, you'll want to go into the properties section and go 337 00:21:10,910 --> 00:21:13,010 down to where it says retargeting. 338 00:21:13,010 --> 00:21:16,520 And you're going to want to change this from default to disabled. 339 00:21:16,640 --> 00:21:21,890 And that should fix your issue with the rotation of the torso of the player. 340 00:21:21,890 --> 00:21:26,810 So other than that, the next thing we need to do is script a system where batteries will spawn around 341 00:21:26,810 --> 00:21:31,850 the map that players can collect to increase the charge of their flashlight when they are low or running 342 00:21:31,850 --> 00:21:32,720 out of batteries. 343 00:21:32,720 --> 00:21:33,860 So I'll go ahead. 344 00:21:33,860 --> 00:21:35,210 See you in the next lecture.